package com.fuyao.cloud.personnel.problem.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyao.cloud.personnel.api.constant.ProblemConstant;
import com.fuyao.cloud.personnel.api.entity.ProblemCti;
import com.fuyao.cloud.personnel.problem.mapper.ProblemCtiMapper;
import com.fuyao.cloud.personnel.problem.service.ProblemCtiService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 问题分类
 *
 * @author pigx code generator
 * @date 2022-04-26 10:13:50
 */
@Service
public class ProblemCtiServiceImpl extends ServiceImpl<ProblemCtiMapper, ProblemCti> implements ProblemCtiService {
    @Override
    public boolean save(ProblemCti entity) {
        setFullFieldValue(entity);
        return super.save(entity);
    }

    @Override
    public boolean updateById(ProblemCti entity) {
        setFullFieldValue(entity);
        return super.updateById(entity);
    }

    private void setFullFieldValue(ProblemCti entity){
        if (entity.getParentId()!=null && entity.getParentId()>0){
            ProblemCti pcti=baseMapper.selectById(entity.getParentId());
            entity.setParentName(pcti.getName());
            entity.setFullcode(pcti.getFullcode()+"."+entity.getCode());
            entity.setFullname(pcti.getFullname()+"."+entity.getName());
        }else {
            entity.setParentId(ProblemConstant.DEFAULT_PARENT_ID);
            entity.setFullcode(entity.getCode());
            entity.setFullname(entity.getName());
        }
    }

    @Override
    public List<Tree<Long>> selectTree() {
        // 查询全部问题分类
        List<ProblemCti> deptAllList = baseMapper.selectList(Wrappers.emptyWrapper());
        // 查询数据权限内部门
//        List<Integer> deptOwnIdList = deptMapper.selectListByScope(Wrappers.emptyWrapper(), new DataScope()).stream()
//                .map(SysDept::getDeptId).collect(Collectors.toList());

        // 权限内部门
        List<TreeNode<Long>> collect = deptAllList.stream()
                .filter(cti -> cti.getId().longValue() != cti.getParentId())
                .map(cti -> {
                    TreeNode<Long> treeNode = new TreeNode();
                    treeNode.setId(cti.getId());
                    treeNode.setParentId(cti.getParentId());
                    treeNode.setName(cti.getName());
//                    treeNode.setWeight(dept.getSort());
                    // 有权限不返回标识
                    Map<String, Object> extra = new HashMap<>(8);
//                    extra.put("isLock", !deptOwnIdList.contains(dept.getDeptId()));
                    extra.put("createTime", cti.getCreateTime());
                    extra.put("code", cti.getCode());
                    treeNode.setExtra(extra);
                    return treeNode;
                }).collect(Collectors.toList());

        return TreeUtil.build(collect, 0L);
    }

//    @Override
//    public IPage selectTreePage(Page page, ProblemCti problemCti) {
//        List<ProblemCti> problemCtiList = this.baseMapper.selectList(Wrappers.<ProblemCti>lambdaQuery().eq(problemCti1 -> problemCti1.getDelFlag(),0));
//        for (int i = 0; i < problemCtiList.size(); i++) {
//
//        }
//        return page;
//    }
}
